package com.ilancuo.file;

import java.io.*;

/**
 * 拷贝文件夹下的所有文件
 * Created by ilancuo on 2018/4/9.
 */
public class CopyTest {

    private static void copyFolder(File src, File dest) throws IOException {
        if (src.isDirectory()) {
            if (!dest.exists()) {
                boolean result = dest.mkdir();
            }
            String[] files = src.list();
            for (String file : files) {
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                copyFolder(srcFile, destFile);
            }
        } else {
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            in.close();
            out.close();
        }
    }

    public static void main(String[] args) {
        try {
            copyFolder(new File("/Users/ilancuo/Downloads"), new File("/Users/ilancuo/dest"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
